package com.bonc.ioc.controller;

import com.alibaba.fastjson.JSONObject;
import com.bonc.ioc.base.controller.BaseController;
import com.bonc.ioc.base.page.PageResult;
import com.bonc.ioc.common.util.AppReply;
import com.bonc.ioc.service.HelpService;
import com.bonc.ioc.validation.AppAuthAnnotation;
import com.bonc.ioc.vo.HelpInfoVo;
import com.bonc.ioc.vo.PageVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author liuyang
 * @creats 2021-04-07-10:37
 */
@RestController
@RequestMapping("/help")
@Slf4j
public class HelpController extends BaseController {
    @Autowired
    HelpService helpService;
    /**
     * 保存 帮助文档
     * @param item 帮助实体
     * @return com.bonc.ioc.common.util.AppReply
     */
    @PostMapping("/save")
    @AppAuthAnnotation
    public AppReply save(@RequestBody HelpInfoVo item){
        return helpService.save(item);
    }

    /**
     * 逻辑删除
     * @param id 主键id
     * @return com.bonc.ioc.common.util.AppReply
     */
    @GetMapping("/delete")
    @AppAuthAnnotation
    public AppReply delete(@RequestParam String id) {
        return helpService.deleteByPrimaryKey(id);
    }

    /**
     * 通过id获取帮助文档内容
     * @return com.bonc.ioc.common.util.AppReply
     */
    @GetMapping("/getHelpInfo")
    public AppReply getStandardInfo(@RequestParam String id) {
        return AppReply.success(helpService.selectByPrimaryKey(Long.valueOf(id)));
    }

    /**
     * 获取分页
     * @return com.bonc.ioc.common.util.AppReply
     * @throws BindException
     */
    @GetMapping("/getHelpByPage")
    public AppReply getHelpInfoByPage(PageVo pageVo) throws BindException {
        HashMap<String,Object> map = new HashMap<>(16);
        map.put("pageSize",pageVo.getPageSize());
        map.put("pageNum",pageVo.getPageNum());
        map.put("title",pageVo.getTitle().trim());
        map.put("content",pageVo.getContent().trim());
        map.put("catalog",pageVo.getCatalog().trim());
        map.put("parentId",pageVo.getParentId());
        map.put("ids",pageVo.getIds());
        PageResult pageResult = helpService.selectHelpInfoByPage(map);
        return AppReply.success(pageResult);
    }

    /**
     * 获取目录
     * @return com.bonc.ioc.common.util.AppReply
     */
    @GetMapping("/getHelp")
    public AppReply getHelp() {
        return AppReply.success(helpService.getHelp());
    }

}
